package cn.demographic.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.demographic.entity.Permissions;
import cn.demographic.entity.Registry;
import cn.demographic.service.PermissionsService;
import cn.demographic.service.RegistryService;
import cn.demographic.util.BaseStaticFinal;


@Controller
public class LoginController {

	@Resource
	private RegistryService registryService;
	@Resource
	private PermissionsService permissionsService;
	
	/**
	 * 登录
	 * @param telephone
	 * @param pwd
	 * @param model
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/dologin")
	public Map<String,Object> getlogin(@RequestParam String telephone,
		@RequestParam String pwd, HttpSession session){
		Registry registry = registryService.getLogin(telephone,pwd);
		Map<String,Object> map= new HashMap<String, Object>();
		if(registry == null) {
			map.put("error","用户名或密码错误");
			return map;
		}
		session.setAttribute("registry", registry);
		session.setAttribute(BaseStaticFinal.USER_SESSION, registry);
		map.put("registry", registry);
		return map;
	}
	
	/**
	 * 权限列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getpermissions")
	public Map<String,Object> dopermissions(){
		List<Permissions> list =permissionsService.getpermissions();
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		return map;
	}
	/**
	 * 注册
	 * 
	 */
	@ResponseBody
	@RequestMapping("/doadd")
	public String addpermissions(String name,String telephone,String pwd,String unit,
								String pcode,String scode,String xiancode,Integer pid){
		int row1=registryService.addregistry(name,telephone,pwd,unit,pid,pcode,scode,xiancode);
		
		return JSON.toJSONString(row1);
		}
	
	/**
	 * 验证手机号吗是否被注册
	 * @param telephone
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/eq")
	public boolean eq(String telephone) {
		boolean error=registryService.gettelephone(telephone);
		return error;
	}
}
